package com.joyxy.spider.mapscript;

import java.util.Arrays;
import com.joyxy.spider.mapscript.operation.BaseOperation;

/**
 * 脚本操作命令
 * 格式：操作数|参数1/参数2/参数3/.../参数N
 */
public class ScriptOperation {
    /** 操作数（操作类型） */
    private int operationType;
    /** 操作参数列表 */
    private int[] parameters;
    
    public ScriptOperation() {
    }
    
    public ScriptOperation(int operationType, int... parameters) {
        this.operationType = operationType;
        this.parameters = parameters;
    }
    
    // Getter and Setter methods
    public int getOperationType() {
        return operationType;
    }
    
    public void setOperationType(int operationType) {
        this.operationType = operationType;
    }
    
    public int[] getParameters() {
        return parameters;
    }
    
    public void setParameters(int[] parameters) {
        this.parameters = parameters;
    }
    
    /**
     * 获取指定索引的参数值
     */
    public int getParameter(int index) {
        if (parameters == null || index < 0 || index >= parameters.length) {
            return 0;
        }
        return parameters[index];
    }
    
    /**
     * 获取参数数量
     */
    public int getParameterCount() {
        return parameters == null ? 0 : parameters.length;
    }
    
    /**
     * 检查操作类型
     */
    public boolean isHorizontalMove() {
        return operationType == OperType.HORIZONTAL_MOVE;
    }
    
    public boolean isVerticalMove() {
        return operationType == OperType.VERTICAL_MOVE;
    }
    
    public boolean isJump() {
        return operationType == OperType.JUMP;
    }
    
    public boolean isMapChange() {
        return operationType == OperType.MAP_CHANGE;
    }
    
    public boolean isLureMonster() {
        return operationType == OperType.LURE_MONSTER;
    }
    
    public boolean isAttack() {
        return operationType == OperType.ATTACK;
    }
    
    /**
     * 检查指定坐标是否在操作区域内
     * 适用于有区域参数的操作（如水平移动、垂直移动、攻击等）
     */
    public boolean isInOperationArea(int x, int y) {
        if (parameters == null || parameters.length < 4) {
            return false;
        }
        
        int xMin = parameters[0];
        int xMax = parameters[1];
        int yMin = parameters[2];
        int yMax = parameters[3];
        
        return x >= xMin && x <= xMax && y >= yMin && y <= yMax;
    }
    
    /**
     * 转换为具体的操作对象
     */
    public BaseOperation toBaseOperation() {
        return BaseOperation.createOperation(operationType, parameters);
    }
    
    /**
     * 从具体操作对象创建ScriptOperation
     */
    public static ScriptOperation fromBaseOperation(BaseOperation operation) {
        // 这里需要根据具体操作类型重新构建参数数组
        // 由于BaseOperation的子类已经包含了所有必要信息，
        // 这个方法主要用于向后兼容
        return new ScriptOperation(operation.getOperationType(), new int[0]);
    }
    
    @Override
    public String toString() {
        return String.format("ScriptOperation{operationType=%d, parameters=%s}", 
                operationType, Arrays.toString(parameters));
    }
} 